// 创建专业相关的小仓库
import { defineStore } from "pinia";
import type { MajorState } from "./types/types";
import type { majorAllResponseData, majorInfoData, majorListFormData, majorListResponseData, majorResponseData } from "@/api/major/type";
import { reqMajorAdd, reqMajorAll, reqMajorById, reqMajorDelete, reqMajorList, reqMajorUpdata } from "@/api/major";




let useMajorStore = defineStore("Major", {
    state: (): MajorState => {
        return {
           majors: []
        }
    },
    actions: {
        // 获取所有专业
        async getMajorAll() {

            let result:  majorAllResponseData = await reqMajorAll()
            if (result.status == 0) {
                this. majors = (result.data as  majorInfoData[])

                return 'ok'
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 分页获取所有专业
        async getMajorList(data:  majorListFormData) {

            let result:  majorListResponseData = await reqMajorList(data);
            if (result.status == 0) {
                this. majors = (result.data?.data as  majorInfoData[])

                return result.data
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 添加专业
        async addMajor(data:  majorInfoData) {

            let result:  majorResponseData = await reqMajorAdd(data);
            if (result.status == 0) {
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 查找所有专业信息（通过id）
        async getMajorById(_id: string) {

            let result:  majorResponseData = await reqMajorById(_id);
            if (result.status == 0) {
                return result.data
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        //  修改专业信息
        async upDataMajor(data: majorInfoData) {

            let result:  majorResponseData = await reqMajorUpdata(data);
            if (result.status == 0) {
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 删除专业
        async deleteMajor( majorId: string) {

            let result:  majorResponseData = await reqMajorDelete( majorId);
            if (result.status == 0) {
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
    }
})
export default useMajorStore